﻿using System;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositorySickList : ADONetRepositoryBase<SickList>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(SickList entity)
        {
            var cmd = new SqlCommand("SickListInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Started", entity.Started));
            cmd.Parameters.Add(new SqlParameter("Finished", entity.Finished));
            logger.Trace("SqlCommand для записи данных Больничного листа в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("SickListDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Больничного листа id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(SickList entity)
        {
            var cmd = new SqlCommand("SickListUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Started", entity.Started));
            cmd.Parameters.Add(new SqlParameter("Finished", entity.Finished));
            logger.Trace("SqlCommand для обновления данных Больничного листа id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetSickList", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Больничного листа id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetSickListByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Больничного листа {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SickList GetEntity(SqlDataReader sqlDataReader)
        {
            SickList sickList = new SickList();
            sickList.Id = (int)sqlDataReader["Id"];
            sickList.Started = (DateTime)sqlDataReader["Started"];
            sickList.Finished = (DateTime)sqlDataReader["Finished"];
            return sickList;
        }
    }
}
